package com.iris.egrant.utils;


import com.iris.egrant.app.utils.IrisStringUtils;
import org.apache.commons.lang.StringUtils;
import org.dom4j.*;
import org.dom4j.io.DOMReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class XMLHelper {
    protected static final Logger LOGGER = LoggerFactory.getLogger(XMLHelper.class);

    public static Document parseDoc(org.w3c.dom.Document doc) {
        if (doc == null) {
            return null;
        }
        DOMReader xmlReader = new DOMReader();
        return xmlReader.read(doc);
    }

    public static void fillXmlDataFromMap(Document docsrc, String xPath, Map resultMap) {
        if (resultMap == null) {
            return;
        }
        Iterator it = resultMap.keySet().iterator();
        while (it.hasNext()) {
            Object obj = it.next();
            String str = "";
            if (resultMap.get(obj) != null) {
                str = String.valueOf(resultMap.get(obj));
            }
            String truePath = xPath + "/" + obj.toString().toLowerCase();
            createXPath(docsrc, truePath);
            Node nd = docsrc.selectSingleNode(truePath);
            Element e = (Element) nd;
            e.setText(str);
        }
    }


    public static Element createXPath(Document docsrc, String xPath) {
        /*  330 */
        String[] paths = StringUtils.split(xPath, "/");
        /*  331 */
        Element parentE = null;
        /*  332 */
        for (String path2 : paths) {
            /*  333 */
            Node nd = null;
            /*  334 */
            if (parentE == null) {
                /*  335 */
                nd = docsrc.selectSingleNode("/" + path2);

            } else {
                /*  337 */
                nd = parentE.selectSingleNode(path2);

            }
            /*  339 */
            if (nd == null) {
                /*  340 */
                if (path2.indexOf("[@") != -1) {
                    /*  341 */
                    String nodeName = path2.substring(0, path2.indexOf("["));
                    /*  342 */
                    String attrName = path2.substring(path2.indexOf("@") + 1, path2.indexOf("="));
                    /*  343 */
                    String attrValue = path2.substring(path2.indexOf("=") + 2, path2.indexOf("]") - 1);
                    /*  344 */
                    if (parentE == null) {
                        /*  345 */
                        parentE = docsrc.addElement(nodeName);

                    } else {
                        /*  347 */
                        parentE = parentE.addElement(nodeName);

                    }
                    /*  349 */
                    parentE.addAttribute(attrName, attrValue);
                    /*  350 */
                    if (parentE != null) {
                        /*  351 */
                        path2 = path2.substring(path2.indexOf("]") + 1);
                        /*  352 */
                        if (path2 != null && path2.length() != 0) {
                            /*  353 */
                            while (path2 != null && path2.length() != 0) {

                                /*  355 */
                                String attrName2 = path2.substring(path2.indexOf("@") + 1, path2.indexOf("="));
                                /*  356 */
                                String attrValue2 = path2.substring(path2.indexOf("=") + 2, path2.indexOf("]") - 1);
                                /*  357 */
                                parentE.addAttribute(attrName2, attrValue2);
                                /*  358 */
                                path2 = path2.substring(path2.indexOf("]") + 1);

                            }

                        }

                    }
                    /*  362 */
                } else if (path2.indexOf("@") == -1) {


                    /*  365 */
                    if (parentE == null) {
                        /*  366 */
                        parentE = docsrc.addElement(path2);

                    } else {
                        /*  368 */
                        parentE = parentE.addElement(path2);

                    }

                }

            } else {
                /*  372 */
                parentE = (Element) nd;

            }

        }
        /*  375 */
        return parentE;

    }

    public static String pingXML(String key, Object obj) {
        /*  691 */
        String value = "";
        /*  692 */
        if (obj != null) {
            /*  693 */
            value = obj.toString();

        }

        /*  696 */
        value = Encoder.encodeHTML(value);
        /*  697 */
        return key + "=\"" + value + "\" ";

    }


    public static void refreshXmlDataFromNode(Document docsrc, String parentPath, Node node) {
        /*  152 */
        refreshXmlDataFromNode(docsrc, parentPath, node, false);

    }


    public static void refreshXmlDataFromNode(Document docsrc, String parentPath, String nodeXML) {
        /*  117 */
        refreshXmlDataFromNode(docsrc, parentPath, nodeXML, false);

    }


    public static void refreshXmlDataFromNode(Document docsrc, String parentPath, String nodeXML, boolean directReplace) {
        /*  128 */
        if ("".equalsIgnoreCase(nodeXML) || "null".equalsIgnoreCase(nodeXML)) {

            return;

        }

        /*  132 */
        Document doc = null;

        try {
            /*  134 */
            doc = DocumentHelper.parseText(nodeXML);

        }
        /*  136 */ catch (DocumentException e) {
            /*  137 */
            LOGGER.error("", (Throwable) e);

        }
        /*  139 */
        Node nd = null;

        /*  141 */
        Element element = doc.getRootElement();
        /*  142 */
        refreshXmlDataFromNode(docsrc, parentPath, (Node) element, true);

    }


    public static void refreshXmlDataFromNode(Document docsrc, String xPath, Node node, boolean directReplace) {
        /*  164 */
        refreshXmlDataFromNode(docsrc, xPath, node, false, true);

    }


    public static void refreshXmlDataFromNode(Document docsrc, String xPath, Node node, boolean directReplace, boolean clearDestIfSecNull) {
        /*  169 */
        Node ndsrc = null;
        /*  170 */
        if (node == null) {
            /*  171 */
            ndsrc = docsrc.selectSingleNode(xPath);
            /*  172 */
            if (clearDestIfSecNull &&
                    /*  173 */         ndsrc != null) {
                /*  174 */
                ndsrc.detach();

            }

            /*  177 */
            createXPath(docsrc, xPath);


            return;

        }
        /*  181 */
        createXPath2(docsrc, xPath);
        /*  182 */
        ndsrc = docsrc.selectSingleNode(xPath);
        /*  183 */
        String nodeName = xPath.substring(xPath.lastIndexOf("/") + 1);
        /*  184 */
        if (nodeName.indexOf("[") > 0) {
            /*  185 */
            nodeName = nodeName.substring(0, nodeName.indexOf("["));
            /*  186 */
        } else if (nodeName.indexOf("@") >= 0) {


        }
        /*  189 */
        node.setName(nodeName);
        /*  190 */
        if (directReplace) {

            /*  192 */
            List<Node> ele = ndsrc.getParent().content();
            /*  193 */
            ele.set(ele.indexOf(ndsrc), node);

        } else {
            /*  195 */
            refreshNode(docsrc, ndsrc, node);

        }

    }


    public static void refreshNode(Document doc, Node ndsrc, Node node) {
        /*  205 */
        Element e = (Element) node;

        /*  207 */
        Element esrc = (Element) ndsrc;




        /*  212 */
        for (int index = 0; index < e.attributeCount(); index++) {
            /*  213 */
            Attribute attr = e.attribute(index);
            /*  214 */
            esrc.addAttribute(attr.getName(), attr.getValue());

        }





        /*  221 */
        if (e.elements().size() > 0) {
            /*  222 */
            Element enew = null;
            /*  223 */
            List<Element> esrcList = esrc.elements();
            /*  224 */
            for (Element esrcc : esrcList) {
                /*  225 */
                esrc.remove(esrcc);

            }
            /*  227 */
            if (e.getName().equalsIgnoreCase(esrc.getName())) {
                /*  228 */
                enew = esrc;

            } else {
                /*  230 */
                enew = esrc.addElement(e.getName());

            }
            /*  232 */
            for (int i = 0; i < e.elements().size(); i++) {
                /*  233 */
                Element nodec = (Element) e.elements().get(i);
                /*  234 */
                Element enewc = enew.addElement(nodec.getName());
                /*  235 */
                refreshNode(doc, (Node) enewc, (Node) nodec);

            }

            /*  238 */
        } else if ((e.elements() == null || esrc.elements().size() == 0) && esrc.getName().equalsIgnoreCase(e.getName())) {

            /*  240 */
            esrc.setText(e.getText());

        }

    }


    public static Element createXPath2(Document docsrc, String xPath) {
        /*  386 */
        String[] paths = StringUtils.split(xPath, "/");

        /*  388 */
        Element parentE = null;

        /*  390 */
        for (String path2 : paths) {

            /*  392 */
            Node nd = null;

            /*  394 */
            if (parentE == null) {

                /*  396 */
                nd = docsrc.selectSingleNode("/" + path2);


            } else {


                /*  402 */
                nd = parentE.selectSingleNode(path2);

            }


            /*  406 */
            if (nd == null) {

                /*  408 */
                if (path2.indexOf("[@") != -1) {
                    /*  410 */
                    String nodeName = path2.substring(0, path2.indexOf("["));

                    /*  412 */
                    String attrName = path2.substring(path2.indexOf("@") + 1, path2.indexOf("="));

                    /*  414 */
                    String attrValue = path2.substring(path2.indexOf("=") + 2, path2.indexOf("]") - 1);

                    /*  416 */
                    if (parentE == null) {

                        /*  418 */
                        parentE = docsrc.addElement(nodeName);

                    } else {

                        /*  422 */
                        parentE = parentE.addElement(nodeName);

                    }


                    /*  426 */
                    parentE.addAttribute(attrName, attrValue);

                    /*  428 */
                    if (parentE != null) {
                        /*  430 */
                        path2 = path2.substring(path2.indexOf("]") + 1);

                        /*  432 */
                        if (path2 != null && path2.length() != 0) {
                            /*  434 */
                            while (path2 != null && path2.length() != 0) {


                                /*  438 */
                                String attrName2 = path2.substring(path2.indexOf("@") + 1, path2.indexOf("="));

                                /*  440 */
                                String attrValue2 = path2.substring(path2.indexOf("=") + 2, path2.indexOf("]") - 1);

                                /*  442 */
                                parentE.addAttribute(attrName2, attrValue2);

                                /*  444 */
                                path2 = path2.substring(path2.indexOf("]") + 1);


                            }


                        }

                    }


                }
                /*  452 */
                else if (path2.indexOf("@") == -1) {




                    /*  458 */
                    if (parentE == null) {
                        /*  460 */
                        parentE = docsrc.addElement(path2);

                    } else {
                        /*  464 */
                        parentE = parentE.addElement(path2);

                    }


                }


            } else {

                /*  472 */
                parentE = (Element) nd;

            }

        }



        /*  478 */
        return parentE;

    }


    public static String parseTableToXML(List<Map<String, Object>> list, String parentNode, String subnode) {
        /*  491 */
        if (list == null || list.size() == 0) {
            /*  492 */
            return "";
        }
        /*  494 */
        StringBuilder xml = new StringBuilder();

        /*  496 */
        if ("".equalsIgnoreCase(subnode)) {

            /*  498 */
            Map map = list.get(0);
            /*  499 */
            xml.append("<" + parentNode + " ");

            /*  501 */
            Iterator it = map.keySet().iterator();
            /*  502 */
            String key = null;
            /*  503 */
            String value = null;
            /*  504 */
            while (it.hasNext()) {
                /*  505 */
                key = it.next().toString().toLowerCase();
                /*  506 */
                value = IrisStringUtils.filterNull(map.get(key));
                /*  507 */
                xml.append(pingXML(key, value));
            }
            /*  509 */
            xml.append("/>");
        } else {
            /*  511 */
            xml.append("<" + parentNode + ">");

            /*  513 */
            for (int i = 0; i < list.size(); i++) {
                /*  514 */
                xml.append("<" + subnode + " ");
                /*  515 */
                Map map = list.get(i);

                /*  517 */
                Iterator it = map.keySet().iterator();
                /*  518 */
                String key = null;
                /*  519 */
                String value = null;
                /*  520 */
                while (it.hasNext()) {
                    /*  521 */
                    key = it.next().toString().toLowerCase();
                    /*  522 */
                    value = IrisStringUtils.filterNull(map.get(key));
                    /*  523 */
                    xml.append(pingXML(key, value));
                }
                /*  525 */
                xml.append("/>");
            }

            /*  528 */
            xml.append("</" + parentNode + ">");
        }
        /*  530 */
        return xml.toString();
    }

    public static org.w3c.dom.Document StringToDoc(String xmlString) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = null;
        org.w3c.dom.Document doc = null;
        try {
            builder = factory.newDocumentBuilder();
            doc = builder.parse(new InputSource(new StringReader(xmlString)));
        } catch (ParserConfigurationException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (SAXException e) {
            throw new RuntimeException(e);
        }
        return doc;

    }


    /*      */
    /*      */
    public static String refreshNodeValue(String xmlData, String xmlPath, Object value) {
        /* 1005 */
        if (IrisStringUtils.isNullOrBlank(xmlData)) {
            /* 1006 */
            return xmlData;
            /*      */
        }
        /* 1008 */
        String strValue = null;
        /* 1009 */
        if (value == null) {
            /* 1010 */
            strValue = "";
            /*      */
        } else {
            /* 1012 */
            strValue = value.toString();
            /*      */
        }
        /* 1014 */
        Document doc = null;
        /*      */
        try {
            /* 1016 */
            doc = DocumentHelper.parseText(xmlData);
            /* 1017 */
        } catch (Exception exception) {
            /* 1018 */
            LOGGER.error("doc转换错误", exception);
            /*      */
        }
        /* 1020 */
        if (doc == null) {
            /* 1021 */
            return xmlData;
            /*      */
        }
        /* 1023 */
        Element e = (Element) doc.selectSingleNode(xmlPath);
        /* 1024 */
        if (e == null) {
            /* 1025 */
            createXPath2(doc, xmlPath);
            /* 1026 */
            e = (Element) doc.selectSingleNode(xmlPath);
            /*      */
        }
        /* 1028 */
        e.setText(strValue);
        /* 1029 */
        return doc.asXML();
        /*      */
    }


}


/* Location:              D:\文件kjt\WEB-INF\lib\egrant-core-2.2.20240205.1659.jar!\com\iris\egran\\utils\XMLHelper.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       1.1.3
 */